package com.ygxsk.carhome.views;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.ygxsk.carhome.R;
import com.ygxsk.carhome.alertview.ItemAdapter;
import com.ygxsk.carhome.alertview.IteminItemClickListener;
import com.ygxsk.carhome.alertview.OperationBean;
import com.ygxsk.carhome.botdialog.BaseBottomDialog;
import com.ygxsk.carhome.listener.OnDialogCancelClickListener;
import com.ygxsk.carhome.listener.OnDialogItemClickListener;

import java.util.ArrayList;

/**
 * Created by itchenqi on 2018/2/2 0002.
 * description：自定义的选择列表弹窗内容
 */

@SuppressLint("ValidFragment")
public class EditListDialog extends BaseBottomDialog {

    //private static Context curContext;
    /**
     * 该弹窗的标题内容
     */
    private String curTitle;

    /**
     * fragment的列表的携带数据内容
     */
    private static final String ARG_DATAS = "arg_datas";
    /**
     * fragment携带的标题的内容标识
     */
    private static final String ARG_TITLE = "arg_title";

    private final ArrayList<OperationBean> mDatas = new ArrayList<>();

    private OnDialogItemClickListener onDialogItemClickListener;

    public void setOnDialogItemClickListener(OnDialogItemClickListener _onDialogItemClickListener) {
        onDialogItemClickListener = _onDialogItemClickListener;
    }

    private OnDialogCancelClickListener onDialogCancelClickListener;

    public void setonCancelListener(OnDialogCancelClickListener _onCancelListener) {
        this.onDialogCancelClickListener = _onCancelListener;
    }

    public EditListDialog() {
        //retain
    }

    /**
     * 2019年09月15日发现的潜在创建fragment的bug内容
     */
    public static EditListDialog newInstance(String _curTitle, //
                                             ArrayList<OperationBean> datas) {

        EditListDialog editListDialog = new EditListDialog();

        Bundle args = new Bundle();
        args.putString(ARG_TITLE, _curTitle);
        args.putParcelableArrayList(ARG_DATAS, datas);

        editListDialog.setArguments(args);

        //LogUtils.d("itchen--EditListDialog-创建--newInstance");

        return editListDialog;

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (getArguments() != null) {
            //curContext = this.getContext();//这种是可以的
            curTitle = getArguments().getString(ARG_TITLE);
            ArrayList<OperationBean> argdatas = getArguments().getParcelableArrayList(ARG_DATAS);
            if ((mDatas != null) && (!mDatas.isEmpty())) {
                mDatas.clear();
            }
            mDatas.addAll(argdatas);
        }

        //LogUtils.d("itchen--EditListDialog---onCreate");

    }

    //2018年09月15日发现bug说是could not find Fragment constructor缺少一个构造函数

    /**
     * 带参数的构造函数
     */
    /*public EditListDialog(Context _curContext, //
                          String _curTitle, //
                          ArrayList<OperationBean> datas) {//

        this.curContext = _curContext;
        this.curTitle = _curTitle;

        if ((mDatas!=null)&&(!mDatas.isEmpty())){
            mDatas.clear();
        }

        mDatas.addAll(datas);

    }*/
    @Override
    public float getDimAmount() {
        return 0.5f;
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public int getLayoutRes() {
        return R.layout.layoutgender;
    }

    @Override
    public void bindView(View viewgender) {

        //第一种方式
        //recyclerView.setOnScrollListener(new ScrollerListener(this));//这有会有空指针的异常风险

        //取消按钮等等
        // View contentView = LayoutInflater.from(this).inflate(R.layout.dialog_content_circle, null);
        //View  viewInGender = CommUtils.getLayoutInflater(MyInfoActivity.this).inflate(R.layout.layoutgender,null,false);

        TextView textViewTitle = viewgender.findViewById(R.id.textviewtitletop);
        TextView textViewCancel = viewgender.findViewById(R.id.textviewgenderCancel);

        textViewTitle.setText(curTitle);
        textViewCancel.setText("取消");

        RecyclerView recyclerView = viewgender.findViewById(R.id.recycleringender);
        //recyclerView.addItemDecoration(new DividerItemDecoration(MyInfoActivity.this, VERTICAL));//这个不添加横线
        recyclerView.setLayoutManager(new LinearLayoutManager(EditListDialog.this.getContext()));

        //LogUtils.d("itchen--recyclerView----列表被创建-----");

        recyclerView.setAdapter(new ItemAdapter(EditListDialog.this.getContext(), mDatas, new IteminItemClickListener() {

            @Override
            public void onNameClickListener(OperationBean position) {

                //LogUtils.d("itchen----OnDialogItemClickListener---->onItemClick");

                if (onDialogItemClickListener != null) {
                    onDialogItemClickListener.onItemClick(position);
                } else {
                    //LogUtils.d("itchen----onDialogItemClickListener==null");
                }

            }

        }));

        //2018年06月30日这两行代码注销因为三星s8和note8的兼容问题=====================================================================
        //第二种方式
        //2018年04月14日屏蔽掉持续滚动
        //recyclerView.addOnScrollListener(new ScrollerListener(curContext));

        //2018年06月30日之前的
        //2018年06月30日代码
        //recyclerView.scrollToPosition(mDatas.size());
        //=======================================================================================

        //2018年06月30日之前的
        //recyclerView.scrollToPosition(mDatas.size() * 1000);//无限滚动

        textViewCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                onDialogCancelClickListener.onCancelClick(0);

            }
        });


    }
}
